
let touchInArea = 0;

const { Event } = Laya;

class Rocker extends Laya.Image {

    constructor() {
        super();
        this.size(206, 206);
        this.pivot(103, 103);
        this.init();
        if (!Rocker.initTochIn) {
            Rocker.initTochIn = true;
            Laya.stage.on(Event.MOUSE_DOWN, this,(e)=> {
                touchInArea = (Laya.stage.mouseX < Laya.stage.width / 2)? 1: 2;
                if (touchInArea==1) {
                    MoveRocker.getInstance().onStart(e);
                }
            });
        }
    }

    init() {
        this.skin = "res/game/rocker/bg.png";
        this.inner = new Laya.Image();
        this.inner.skin  = "res/game/rocker/stick_move.png";
        this.inner.pivot(this.inner.width/2, this.inner.height/2);
        this.inner.pos(this.width/2, this.height/2);
        this.addChild(this.inner);
    }

    onStart(e) {
        this._touchId = e.touchId;
        this.resetPos = [this.x, this.y];
        if (this.touchInArea == 1) {
            this.pos(Laya.stage.mouseX, Laya.stage.mouseY);
        } else {
            this.onMove(e);
        }
        Laya.stage.on(Laya.Event.MOUSE_MOVE, this, this.onMove);
        Laya.stage.on(Laya.Event.MOUSE_OUT, this, this.stop, [false]);
        Laya.stage.on(Laya.Event.MOUSE_UP, this, this.stop, [true]);
    }

    onMove(e) {
        if (e.touchId != this._touchId) return;

        let r = this.width / 2;
        let disX = this.mouseX - r;
        let disY = this.mouseY - r;
        let dis = Math.sqrt(Math.pow(disX, 2) + Math.pow(disY, 2));

        let n = Math.min(r / dis, 1);
        let changeX = n * disX;
        let changeY = n * disY;

        this.inner.x = this.width/2 + changeX;
        this.inner.y = this.height/2 + changeY;

        let direct = [Math.max(-1, Math.min(1, disX/r)), Math.max(-1, Math.min(1, disY/r))];
        let speed = Math.min(dis / r, 1);
        direct.map(v=> Math.round(v));
        this.direct = direct;
        // let angle = Math.atan2(disY, disX) * 180 / Math.PI;
        this.event('ROKER_MOVE', [direct]);
    }

    stop(isExecute, e) {
        if (e.touchId != this._touchId) return;

        if (!isExecute) this.speed = 0;

        this.event('ROKER_EXECUTE', [this.direct]);

        this.inner.pos(this.width / 2, this.height / 2);
        this.pos(this.resetPos[0], this.resetPos[1]);

        Laya.stage.off(Laya.Event.MOUSE_MOVE, this, this.onMove);
        Laya.stage.off(Laya.Event.MOUSE_OUT, this, this.stop);
        Laya.stage.off(Laya.Event.MOUSE_UP, this, this.stop);
    }


}

/**
 * 移动摇杆
 */
export class MoveRocker extends Rocker {

    static getInstance() {
        if (!MoveRocker.instance||MoveRocker.instance.destroyed) MoveRocker.instance = new MoveRocker();
        return MoveRocker.instance;
    }

    constructor() {
        super();
        this.touchInArea = 1;
    }

}

/**
 * 攻击摇杆
 */
export class AttackRocker extends Rocker {

    static getInstance() {
        if (!AttackRocker.instance||AttackRocker.instance.destroyed) AttackRocker.instance = new AttackRocker();
        return AttackRocker.instance;
    }

    constructor() {
        super();
        this.touchInArea = 2;
        this.touchInType = 2;
        this.inner.skin  = "res/game/rocker/stick_aim.png";

        this.on(Event.MOUSE_DOWN, this,(e)=> {
            this.onStart(e);
        });
    }

    setPower(val) {

    }
    
}

/**
 * 技能摇杆
 */
export class SkillRocker extends Rocker {

    static getInstance() {
        if (!SkillRocker.instance||SkillRocker.instance.destroyed) SkillRocker.instance = new SkillRocker();
        return SkillRocker.instance;
    }

    constructor() {
        super();
        /*this.size(206, 206);
        this.pivot(103, 103);*/

        this.touchInArea = 2;
        this.touchInType = 3;

        this.skin = "res/game/rocker/bg_skill.png";
        this.inner.skin  = "res/game/rocker/charge_skill.png";
        this.inner.pivot(85/2, 85/2);

        this.on(Event.MOUSE_DOWN, this,(e)=> {
            this.onStart(e);
        });
    }
    
}
